package com.flyoil.petromp.ui.fragment.fragment_order;

import android.content.Intent;

import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.R;
import com.flyoil.petromp.adapter.adapter_service.ItemProjectServiceAdapter;
import com.flyoil.petromp.apiservice.HttpModel;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.base.BaseFragment;
import com.flyoil.petromp.base.BaseRecyAdapter;
import com.flyoil.petromp.entity.entity_service.ProjectListEntity;
import com.flyoil.petromp.entity.entity_service.ProjectServiceEntity;
import com.flyoil.petromp.entity.entity_service.SupplierServiceListEntity;
import com.flyoil.petromp.ui.activity.activity_repertory.ServiceMessageActivity;
import com.flyoil.petromp.ui.activity.activity_repertory.SupplierMessageActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by songzhiyin on 2017/11/2.
 * 服务列表的fragment
 */

public class ServiceListFragment extends BaseFragment {

    private ItemProjectServiceAdapter adapter;

    @Override
    protected int getContentViewResId() {
        return R.layout.fragment_project_service_list;
    }

    @Override
    protected void initViews() {
        super.initViews();
        recyclerView = $(R.id.list_fragment_project_service_list);
        setRecyviewLayoutManager(null);
        adapter = new ItemProjectServiceAdapter(mContext);
        recyclerView.setAdapter(adapter);
        httpModel = new HttpModel();
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        adapter.setOnItemClickListener(onItemClickListener);
        setOnRefreshListener(true);
    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        initdatas();
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        initdatas();
    }
    @Override
    protected void initdatas() {
        super.initdatas();
        Map<String, Object> map = new HashMap<>();
        map.put("pageNumber", pageNumber);
        map.put("pageSize", pageSize);
        httpModel.getSupplierServiceList(map, new OnObserverRetrofitResetCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity model) {
                super.onSuccess(model);
                SupplierServiceListEntity entity = null;
                if (model instanceof SupplierServiceListEntity) {
                    entity = (SupplierServiceListEntity) model;
                }
                if (entity != null && entity.getData() != null && entity.getData() != null && model.getCode() == ConstantNetworkCode.SuccedCode) {
                    final List<ProjectServiceEntity> projectServiceEntities = new ArrayList<>();
                    projectServiceEntities.clear();
                    for (int i = 0; i < entity.getData().getList().size(); i++) {
                        ProjectServiceEntity item = new ProjectServiceEntity();
                        item.setId(entity.getData().getList().get(i).getId());
                        item.setName(entity.getData().getList().get(i).getName());
                        item.setType(entity.getData().getList().get(i).getSpecification());
                        projectServiceEntities.add(item);
                    }
                    if (isFirstLoadck) {
                        isFirstLoadck = false;
                        adapter.addBottonDatas(projectServiceEntities);
                        recyclerView.notifyDataSetChanged();
                        if (entity.getData().getList().size() > 0) {
                            onDataSucceed();
                            pageNumber++;
                        } else {
                            onDataNull(null);
                        }
                    } else {
                        if (pageNumber == 1) {
                            adapter.setDataList(projectServiceEntities);
                            recyclerView.notifyDataSetChanged();
                            onRefreshDataSucceed();
                        } else {
                            adapter.addBottonDatas(projectServiceEntities);
                            recyclerView.notifyDataSetChanged();
                            if (entity.getData().getList().size() < pageSize) {
                                onLoadDataFinish();
                            } else {
                                onLoadDataSucceed();

                            }
                        }
                        pageNumber++;
                    }

                } else {
                    if (isFirstLoadck) {
                        onDataNull(null);
                    } else {
                        if (pageNumber == 1) {
                            onRefreshDataError();
                        } else {
                            onLoadDataError();
                        }
                    }

                }
            }

            @Override
            public void onFailure(String msg) {
                onDataError(null);
            }
        });
    }

    private BaseRecyAdapter.OnItemClickListener onItemClickListener = new BaseRecyAdapter.OnItemClickListener() {
        @Override
        public void Onclick(int position, Object data) {
            Intent intent = new Intent(mContext, ServiceMessageActivity.class);
            ProjectServiceEntity entity = adapter.getItemData(position);
            intent.putExtra("id", entity.getId());
            intent.putExtra("show_siganture",false);//不显示签字意见部分的ui
            startActivity(intent);
        }
    };
}
